﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using EpicsJecketClient;
using System.Text.RegularExpressions;
using jtext_EpicsClient;
using UDPE;
using string_short;

namespace EJClientTester
{
    
    public partial class Form1 : Form
    {
        bool isByte;
        udpe ud = new udpe();
        byte[] bmsg;
        public Form1()
        {
            InitializeComponent();
        }

        //#region event handler
        ////private void Ivk_Arrived(object message)
        ////{
        ////    msg = message;
        ////    MethodInvoker my_fresh = new MethodInvoker(msgArrived);
        ////    this.BeginInvoke(my_fresh);
        ////}

        ////private void Ivk_Acked(object state)
        ////{
        ////    MethodInvoker my_fresh = new MethodInvoker(msgAcked);
        ////    this.BeginInvoke(my_fresh);
        ////}
        
        
        ///// <summary>
        ///// show message when my message is acked.
        ///// </summary>
        ///// <param name="state">The state.</param>
        ////private void msgAcked()
        ////{
        ////    TBrecieve.Text += Environment.NewLine + System.DateTime.Now.ToString() + " message Acked  ";
        ////}

        ///// <summary>
        ///// show message whenmessage arriverd
        ///// </summary>
        ///// <param name="msg">The MSG.</param>
        //private void msgArrived()
        //{
        //    string StrShow;
        //    if (isByte == true)
        //    {
        //        StrShow = BytesToHexString((byte[])msg, ";");
        //    }
        //    else 
        //    {
        //        StrShow=(string)msg;
        //    }
        //    TBrecieve.Text += Environment.NewLine + System.DateTime.Now.ToString() + "  message Arrived  " + StrShow;
        //}

        //#endregion


     

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

      
      

        private void BTSend_Click(object sender, EventArgs e)
        {
            //object ipp=(object)IPTextbox.Text;
            //ud.SetDriver(true, -1, 0, ipp);
            //ud.IniDriver();
            //isByte = CBByte1.Checked;
            //try
            //{
                if (isByte == true)
                {
                    byte[] msg;
                    msg = HexStringToBytes(TBsent.Text, ";");
                    //myEjClient.Msg_send(msg).ToString();
                    ud.Send_msg(msg);
                }
                else
                {
                    ud.Send_msg(TBsent.Text);
                }


            //}
            //catch
            //{
            //    MessageBox.Show("message format error");
            //}

            //try
            //{
            //    string s = ud.ReceiveMessageF();
            //    ReceiverichTextBox.AppendText(s + "\n");
            //}
            //catch
            //{
            //    MessageBox.Show("Can't receive");
            //}
            //ud.StopDriver();
        }

        #region additionnal
        /// <summary>
        /// make a string that contains many hex numbers splited by spliter to byte[]
        /// </summary>
        /// <param name="HexString">The hex string.</param>
        /// <param name="spliter">The spliter.</param>
        /// <returns></returns>
        public static byte[] HexStringToBytes(string HexString,string spliter)
        {
            byte[] tBytes;
            string[] tVals = Regex.Split(HexString, spliter , RegexOptions.IgnoreCase);
            tBytes = new byte[tVals.Length];
            for (int i = 0; i < tVals.Length; i++)
            {
                tBytes[i] = Convert.ToByte(tVals[i], 16);
            }
            return tBytes;
        }

        /// <summary>
        /// make byte array to a string .
        /// </summary>
        /// <param name="BytesArray">The bytes array.</param>
        /// <param name="spliter">The spliter.</param>
        /// <returns></returns>
        public static string BytesToHexString(byte[] BytesArray, string spliter)
        {
            string StrOut="";
            for (int m = 0; m < BytesArray.Length; m++)
            {
                StrOut=StrOut + (BytesArray[m].ToString("X2") + spliter);
            }
            return StrOut;
        }

        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            object ipp = (object)IPTextbox.Text;
            ud.SetDriver(true, -1, 0, ipp);
            ud.IniDriver();
            ud.OnSendToEJServer+=new JtextEpicsInterface.SendToEJServerHandler(test);
            isByte = CBByte1.Checked;
            ud.ListenerThread.IsBackground = true;
            
        }

        private void test(byte[] msg)
        {
            bmsg = msg;
            MethodInvoker my_fresh = new MethodInvoker(uiUpdate);
            this.BeginInvoke(my_fresh);

        }
        private void uiUpdate()
        {
            if (isByte == true)
            {
                text2.Text = BytesToHexString(bmsg, ";");
            }
            else
            {
                sbyte[] sb = Str_Short.ByteToSByte(bmsg);
                text2.Text = Str_Short.SByteToString(sb);
            }
        }

        private void Form1Closing(object sender,System.ComponentModel.CancelEventArgs e)
        {
            ud.StopListener();
            ud.StopDriver();
        }

        private void text2_TextChanged(object sender, EventArgs e)
        {

        }
      



    }
}
